# Windows Terminal 安装检查脚本
# 检查系统状态和依赖项

Write-Host "🔍 Windows Terminal 安装检查" -ForegroundColor Cyan
Write-Host "=============================" -ForegroundColor White
Write-Host ""

# 检查 Windows 版本
Write-Host "📋 系统信息:" -ForegroundColor Yellow
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
Write-Host "  操作系统: $($osInfo.Caption)" -ForegroundColor White
Write-Host "  版本: $($osInfo.Version)" -ForegroundColor White
Write-Host "  架构: $($osInfo.OSArchitecture)" -ForegroundColor White

# 检查 PowerShell 版本
Write-Host "  PowerShell: $($PSVersionTable.PSVersion)" -ForegroundColor White
Write-Host ""

# 检查 Windows Terminal
Write-Host "📋 Windows Terminal 状态:" -ForegroundColor Yellow
$wtInstalled = $false
$wtPath = Get-Command wt -ErrorAction SilentlyContinue
$appxPackage = Get-AppxPackage -Name "Microsoft.WindowsTerminal" -ErrorAction SilentlyContinue

if ($wtPath -ne $null) {
    Write-Host "  ✅ 已安装" -ForegroundColor Green
    Write-Host "  📍 路径: $($wtPath.Source)" -ForegroundColor White
    $wtInstalled = $true
} elseif ($appxPackage -ne $null) {
    Write-Host "  ✅ 已安装 (UWP)" -ForegroundColor Green
    Write-Host "  📦 包名: $($appxPackage.PackageFullName)" -ForegroundColor White
    Write-Host "  🏷️  版本: $($appxPackage.Version)" -ForegroundColor White
    $wtInstalled = $true
} else {
    Write-Host "  ❌ 未安装" -ForegroundColor Red
}

Write-Host ""

# 检查 Windows Terminal 配置
Write-Host "📋 Windows Terminal 配置:" -ForegroundColor Yellow
$settingsDir = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState"
$settingsFile = "$settingsDir\settings.json"

if (Test-Path $settingsFile) {
    Write-Host "  ✅ 配置文件存在" -ForegroundColor Green
    Write-Host "  📍 路径: $settingsFile" -ForegroundColor White
} else {
    Write-Host "  ❌ 配置文件不存在" -ForegroundColor Red
}

Write-Host ""

# 检查 PowerShell 配置
Write-Host "📋 PowerShell 配置:" -ForegroundColor Yellow
$profileExists = Test-Path $PROFILE.CurrentUserCurrentHost

if ($profileExists) {
    Write-Host "  ✅ PowerShell 配置文件存在" -ForegroundColor Green
    Write-Host "  📍 路径: $PROFILE.CurrentUserCurrentHost" -ForegroundColor White
} else {
    Write-Host "  ❌ PowerShell 配置文件不存在" -ForegroundColor Red
}

Write-Host ""

# 检查 Unicode 支持
Write-Host "📋 Unicode 支持:" -ForegroundColor Yellow
Write-Host "  输出编码: $([Console]::OutputEncoding.EncodingName)" -ForegroundColor White
Write-Host "  输入编码: $([Console]::InputEncoding.EncodingName)" -ForegroundColor White
Write-Host "  代码页: $([Console]::OutputEncoding.CodePage)" -ForegroundColor White

# 检查代码页
try {
    $chcp = chcp
    $currentCodePage = $chcp -replace "[^0-9]", ""
    Write-Host "  当前代码页: $currentCodePage" -ForegroundColor White

    if ($currentCodePage -eq "65001") {
        Write-Host "  ✅ 代码页已设置为 UTF-8" -ForegroundColor Green
    } else {
        Write-Host "  ⚠️  代码页不是 UTF-8 (65001)" -ForegroundColor Yellow
    }
} catch {
    Write-Host "  ❌ 无法获取代码页信息" -ForegroundColor Red
}

Write-Host ""

# 检查依赖项
Write-Host "📋 依赖项检查:" -ForegroundColor Yellow

# 检查 winget
$winget = Get-Command winget -ErrorAction SilentlyContinue
if ($winget -ne $null) {
    Write-Host "  ✅ winget 可用" -ForegroundColor Green
} else {
    Write-Host "  ❌ winget 不可用" -ForegroundColor Red
}

# 检查 Python
$python = Get-Command python -ErrorAction SilentlyContinue
if ($python -ne $null) {
    Write-Host "  ✅ Python 可用" -ForegroundColor Green
    Write-Host "  📍 路径: $($python.Source)" -ForegroundColor White
} else {
    Write-Host "  ❌ Python 不可用" -ForegroundColor Red
}

# 检查 Git
$git = Get-Command git -ErrorAction SilentlyContinue
if ($git -ne $null) {
    Write-Host "  ✅ Git 可用" -ForegroundColor Green
    Write-Host "  📍 路径: $($git.Source)" -ForegroundColor White
} else {
    Write-Host "  ❌ Git 不可用" -ForegroundColor Red
}

Write-Host ""

# 检查网络连接
Write-Host "📋 网络连接:" -ForegroundColor Yellow
try {
    $connection = Test-Connection -ComputerName "www.microsoft.com" -Count 1 -Quiet
    if ($connection) {
        Write-Host "  ✅ 网络连接正常" -ForegroundColor Green
    } else {
        Write-Host "  ❌ 网络连接失败" -ForegroundColor Red
    }
} catch {
    Write-Host "  ❌ 网络连接检查失败" -ForegroundColor Red
}

Write-Host ""

# 测试 Unicode 输出
Write-Host "🧪 Unicode 测试:" -ForegroundColor Yellow

$unicodeTests = @(
    @{"name" = "ASCII"; "text" = "Hello World"},
    @{"name" = "中文"; "text" = "你好世界"},
    @{"name" = "Emoji"; "text" = "✅ 🎉 🚀"}
)

foreach ($test in $unicodeTests) {
    Write-Host "  $($test.name): " -ForegroundColor Yellow -NoNewline
    try {
        Write-Host $test.text -ForegroundColor Green
    } catch {
        Write-Host "❌ FAILED" -ForegroundColor Red
    }
}

Write-Host ""

# 显示建议
Write-Host "💡 建议和下一步操作:" -ForegroundColor Cyan
Write-Host ""

if (-not $wtInstalled) {
    Write-Host "📦 安装 Windows Terminal:" -ForegroundColor Yellow
    Write-Host "  • 运行: .\install.ps1" -ForegroundColor White
    Write-Host "  • 或从 Microsoft Store 手动安装" -ForegroundColor White
    Write-Host ""
}

if (-not (Test-Path $settingsFile)) {
    Write-Host "⚙️  配置 Windows Terminal:" -ForegroundColor Yellow
    Write-Host "  • 运行: .\setup_profile.ps1" -ForegroundColor White
    Write-Host ""
}

if ($currentCodePage -ne "65001") {
    Write-Host "🔧 修复 Unicode 支持:" -ForegroundColor Yellow
    Write-Host "  • 运行: .\fix_unicode.ps1" -ForegroundColor White
    Write-Host ""
}

Write-Host "🚀 可用脚本:" -ForegroundColor Yellow
Write-Host "  • install.ps1          - 完整安装脚本" -ForegroundColor White
Write-Host "  • quick_start.bat      - 快速启动脚本" -ForegroundColor White
Write-Host "  • fix_unicode.ps1       - Unicode 修复脚本" -ForegroundColor White
Write-Host "  • setup_profile.ps1     - PowerShell 配置脚本" -ForegroundColor White
Write-Host "  • test_unicode.py       - Unicode 测试工具" -ForegroundColor White
Write-Host "  • check_installation.ps1 - 安装检查脚本" -ForegroundColor White

Write-Host ""
Write-Host "🎯 检查完成！" -ForegroundColor Cyan